﻿using Microsoft.AspNetCore.Mvc;
using System;
using bpms.Enums;
using bpms.Service;

namespace bpms.Controllers
{
    /// <summary>
    /// 订单模块
    /// </summary>
    public class OrderController : BaseController
    {
        private readonly OrderService _orderService;

        public OrderController(OrderService orderService)
        {
            _orderService = orderService;
        }
        public IActionResult Index(string keyword)
        {
            //如果是管理员用户，可以查看所有订单
            if (CurrentUser.UserType == UserType.Admin)
            {
                var orders = _orderService.GetAll(keyword);
                return View(orders);
            }
            else
            {
                //如果是买家用户，只能查看自己的订单
                var orders = _orderService.GetBuyerAll(CurrentUser.Id, keyword);
                return View(orders);
            }
        }

        public IActionResult Details(int? id)
        {
            if (id == null) return NotFound();

            var order = _orderService.GetById(id.Value);

            if (order == null) return NotFound();

            return View(order);
        }

        public IActionResult Delete(int id)
        {
            _orderService.Delete(id);
            return RedirectToAction("Index");
        }
    }
}
